<?php 
class DbHelper
{
  private $pdo;
  protected $res;
  protected $config;
  public function __construct()
  {
    
  }
  private function getCon()
  {
    if(file_exists(dirname(__FILE__)."/config.php"))
    require(dirname(__FILE__)."/config.php");
    else
    {
      echo "<script>window.location.href='../login.php'</script>";
      return;
    }
    try
    {
      $this->pdo = new PDO($pdoConfig['dsn'],$pdoConfig['username'],$pdoConfig['pwd'],array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES 'utf8'"));
      $this->pdo->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
    }
    catch(PDOException $ex)
    { 
     try
     {
        $this->pdo = new PDO($rConfig['dsn'],$rConfig['username'],$rConfig['pwd'],array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES 'utf8'"));
        //mysql_query("SET NAMES 'UTF8'");
        //mysql_query("SET CHARACTER SET UTF8");
        //mysql_query("SET CHARACTER_SET_RESULTS=UTF8'");


        $this->pdo->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION); 
      }
     catch(PDOException $e)
     {
        throw $e;
      }
     }    
  }
  public function __destruct()
  {
    if(isset($this->pdo))
    {
      $this->pdo=null;
      unset($this->pdo);
    }
    if(isset($this->stmt))
    {
      $this->pdo = null;
      unset($this->pdo);
    }
  }
  //以下为查询操作
  
  /**
   *返回单条数据，利用fetch，并使用默认参数
   *@param $sql 参数化查询语句
   *@param $value 参数化查询参数值,类型为数组,默认为空
   *@return 第一条数据
   */
  public function getRecord($sql,$value=null)
  {
    try
    {
      $this->getCon();
      $stmt = $this->pdo->prepare($sql);
      if($stmt->execute($value))
      {
        return $stmt->fetch();
      }    
    }
    catch(Exception $ex)
    {
      throw $ex;
    }
  }
  
  /**
   *返回单条数据，利用fetchAll,并使用默认参数
   *@param $sql 参数化查询语句
   *@param $value 参数化查询参数值
   *@param $ftype 参数化查询类型，默认值为PDO::FETCH_BOTH
   *@return 所有数据
   */
  public function getRecords($sql,$value,$ftype=PDO::FETCH_BOTH)
  {
    try
    {
      $this->getCon();
      $stmt = $this->pdo->prepare($sql);
      if($stmt->execute($value))
      {
        return $stmt->fetchALL($ftype);
      }
    }
    catch(Exception $ex)
    {
      throw $ex;
    }
  }
  
  
  //以下为更新操作包括create和update
  
  /**
   *插入数据，不需要使用事务
   *@param $sql the sql statement to be executed
   *@param $value the value of the parameter
   *@return  be effected rows;
   */
  public function insertRecord($sql,$value=null)
  {
    try
    {
      $this->getCon();
      $stmt = $this->pdo->prepare($sql);
      $stmt->execute($value);
      return $stmt->rowCount();
    }
    catch(PDOException $ex)
    {
      throw $ex;
    }
  }
  
  /**
   *插入多条数据，需要用到事务
   */
  public function insertRecords($sqls,$values)
  {
    try
    {
      $this->getCon();
      $i=0;
      $this->pdo->beginTransaction();
      foreach($sqls as $sql)
      {
        $stmt = $this->pdo->prepare($sql);
        $stmt->execute($values[$i++]);        
      }
      $this->pdo->commit();
    }
    catch(PDOException $ex)
    {
      $this->pod->rollback();
      throw $ex;
      //return -1;
    }
  }
  
  /**
   *update a record
   */
  public function updateRecord($sql,$value=null)
  {
    $this->insertRecord($sql,$value);
  }
  //以下为删除操作
  /**
   *delete the record by the sql statement
   *@param $sql sql statement
   *@return 返回影响条数
   */
  public function delRecord($sql,$value=null)
  {
    try
    {
      $this->getCon();
      $stmt = $this->pdo->prepare($sql);
      $stmt->execute($value);
      return $stmt->rowCount();
    }
    catch(PDOException $ex)
    {
      throw $ex;
    }
  }
  /*
  public function delRecord($sql)
  {
    $count = $this->pdo->exec($sql);
    return $count;
  }
  */
  /**
   *删除多条数据
   */
  public function delRecords($sqls)
  {
    
  }

  //其他操作
}
 ?>